﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;
using JtextEpicsInterface;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Serialization;
using Microsoft.Scripting;

namespace JEPy
{
    public class PythonExe
    {
        ScriptEngine PyEng;
        ScriptScope PySc ;
        Hashtable codeTable;
        /// <summary>
        /// Initializes a new instance of the <see cref="PythonExe"/> class.
        /// </summary>
        public PythonExe()
        {

        }


        /// <summary>
        /// Initialie the pyexe, build scope here.
        ///  If you want to clear the scope, call this
        /// </summary>
        public void IniPyExe()
        {
            PyEng = Python.CreateEngine();
            PySc = PyEng.CreateScope();
            codeTable = new Hashtable();
        }

        /// <summary>
        /// Sets the varible value in the scope.
        /// </summary>
        /// <param name="Name">The name.</param>
        /// <param name="value">The value.</param>
        public void SetVarible(string vName, Object vValue)
        {
            PySc.SetVariable(vName, vValue);
        }

        /// <summary>
        /// Gets the varible in the scope.
        /// </summary>
        /// <param name="Name">The name.</param>
        /// <returns></returns>
        public object GetVarible(string vName)
        {
            try
            {
                return PySc.GetVariable(vName);
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// Adds the python code from file.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <param name="CodeName">Name of the code.</param>
        public void addCodeFromFile(string path,string CodeName)
        {
            ScriptSource tCode = PyEng.CreateScriptSourceFromFile(path, Encoding.ASCII , SourceCodeKind.Statements);
            codeTable.Add( CodeName,tCode);
        }

        /// <summary>
        /// Exes the code.
        /// </summary>
        /// <param name="CodeName">Name of the code.</param>
        public void ExeCode(string CodeName)
        {
            ((ScriptSource)(codeTable[CodeName])).Execute(PySc);
        }

        /// <summary>
        /// Executes code from file.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <returns></returns>
        public object ExcFromFile(string path)
        {
            return PyEng.ExecuteFile(path, PySc);
        }
        /// <summary>
        /// evalue or execute a code 
        /// </summary>
        /// <param name="Expr">The expr.</param>
        /// <returns></returns>
        public object ExcMe(string Expr)
        {
            return PyEng.Execute(Expr, PySc);
        }

    }
}
